#include "flattingwindow.h"

#include "audiostatusmonitor.h"
#include "dropmanager.h"
#include "imagestatusmonitor.h"
#include "textstatusmonitor.h"
#include "toolutils.h"

FlattingWindow::FlattingWindow(PagesWidget* pagesWidget, QWidget* parent)
    :QWidget(parent)
{
    mDroppableWidget = new DroppableWidget(this);
    DropManager::GetInstance()->AddDroppableWidget(mDroppableWidget);
    mDroppableWidget->installEventFilter(DropManager::GetInstance());
    mLayout = new QVBoxLayout(this);
    mLayout->addWidget(mDroppableWidget);
    mDroppableWidget->AddPagesWidget(pagesWidget);
    Init();
    ConnectAll();
}

FlattingWindow::~FlattingWindow()
{
    DropManager::GetInstance()->RemoveDroppableWidget(mDroppableWidget);
}

FlattingWindow::FlattingWindow(const std::pair<DragableTab*, QWidget*> &page, QWidget* parent)
    :QWidget(parent)
{
    mDroppableWidget = new DroppableWidget(this);
    DropManager::GetInstance()->AddDroppableWidget(mDroppableWidget);
    mDroppableWidget->installEventFilter(DropManager::GetInstance());
    mLayout = new QVBoxLayout(this);
    mLayout->addWidget(mDroppableWidget);
    mDroppableWidget->AddPageToFocusPagesWidget(page.first, page.second);
    Init();
    ConnectAll();
}

void FlattingWindow::Init() {
    QIcon icon(QIcon::fromTheme(QIcon::ThemeIcon::Scanner));
    this->setWindowIcon(icon);
    this->setWindowFlags(Qt::Widget /*| Qt::WindowStaysOnTopHint*/);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->resize(500, 500);

    mStatusBar = new QStatusBar(this);
    mLayout->addWidget(mStatusBar);

    mLayout->setContentsMargins(0, 0, 0, 0);
    mLayout->setSpacing(0);
    mStatusBar->setStyleSheet("QStatusBar::item { border-right: 0.5px solid rgb(50, 50, 50); }"); // 改变分隔符颜色
    mStatusBar->setStyleSheet("QStatusBar::item { border: none; }"); // 去掉分割符
    mStatusBar->setFixedHeight(30);    // 调整高度，组件太小影响status的高度
    OnFocusPageChangedSlots();
    // actions
    mGlobalAction = QSharedPointer<GlobalAction>::create(this);
}

void FlattingWindow::ConnectAll() {
    connect(mDroppableWidget, &DroppableWidget::FocusPageChanged, this, &FlattingWindow::OnFocusPageChangedSlots);
    connect(mDroppableWidget, &DroppableWidget::AllPagesClosed, this, &QWidget::close);

    // actions
    connect(mGlobalAction->actionCreatePage.get(), &QAction::triggered, this, [this] {
        std::string s = "无标题";
        mDroppableWidget->AddPageToFocusPagesWidget(
        QString::fromStdString(s), new QPlainTextEdit(this), DragableTab::TabType::TEXT);
    });
    connect(mGlobalAction->actionClosePage.get(), &QAction::triggered, this, [this] {
        mDroppableWidget->RemoveFocusPage();
    });
    connect(mGlobalAction->actionOpenFile.get(), &QAction::triggered, this, [this] {
        auto [tab, content] = ToolUtils::OpenPage(this);
        mDroppableWidget->AddPageToFocusPagesWidget(tab, content);
    });
    connect(mGlobalAction->actionSaveFile.get(), &QAction::triggered, this, [this] {
        ToolUtils::SavePage(mDroppableWidget->FocusPage(), this);
    });
    connect(mGlobalAction->actionSaveAsFile.get(), &QAction::triggered, this, [this] {
        ToolUtils::SavePageAs(mDroppableWidget->FocusPage(), this);
    });
}

void FlattingWindow::OnFocusPageChangedSlots() {
    if (mStatusMonitor != nullptr) {
        mStatusMonitor->close();
        mStatusBar->removeWidget(mStatusMonitor);
        delete mStatusMonitor;
        mStatusMonitor = nullptr;
    }

    auto [fst, snd] = mDroppableWidget->FocusPage();
    if (fst->ContentType() == DragableTab::TabType::TEXT){
        mStatusMonitor = new TextStatusMonitor(qobject_cast<QPlainTextEdit*>(snd), this);
        mStatusBar->addWidget(mStatusMonitor, 1);
    }

    if (fst->ContentType() == DragableTab::TabType::IMAGE){
        mStatusMonitor = new ImageStatusMonitor(qobject_cast<ImageEditor*>(snd), this);
        mStatusBar->addWidget(mStatusMonitor, 1);
    }

    if (fst->ContentType() == DragableTab::TabType::MUSIC) {
        mStatusMonitor = new audioStatusMonitor(qobject_cast<SVideoWidget*>(snd), this);
        mStatusBar->addWidget(mStatusMonitor, 1);
    }

    // BuildTextPageActionSignalSlots(snd, fst->ContentType());
}
